import { Injectable, signal } from '@angular/core';

export interface SelectionState {
  selectedTables: string[];
  selectedNotes: string[];
  selectedAreas: string[];
}

@Injectable({
  providedIn: 'root'
})
export class SelectionService {
  private _selection = signal<SelectionState>({
    selectedTables: [],
    selectedNotes: [],
    selectedAreas: []
  });

  readonly selection = this._selection.asReadonly();

  selectTable(id: string, multiSelect = false) {
    this._selection.update(current => ({
      ...current,
      selectedTables: multiSelect
        ? [...current.selectedTables, id]
        : [id],
      selectedNotes: [],
      selectedAreas: []
    }));
  }

  selectNote(id: string, multiSelect = false) {
    this._selection.update(current => ({
      ...current,
      selectedNotes: multiSelect
        ? [...current.selectedNotes, id]
        : [id],
      selectedTables: [],
      selectedAreas: []
    }));
  }

  selectArea(id: string, multiSelect = false) {
    this._selection.update(current => ({
      ...current,
      selectedAreas: multiSelect
        ? [...current.selectedAreas, id]
        : [id],
      selectedTables: [],
      selectedNotes: []
    }));
  }

  clearSelection() {
    this._selection.set({
      selectedTables: [],
      selectedNotes: [],
      selectedAreas: []
    });
  }
}
